'use strict';

const express = require('express');
const dbutility = require('../../common/dbutility');
const gputility = require('../../common/gputility');
const logger = require('../../common/logutility').logger('srhCouLst');
let router = express.Router();

router.get('/', function(req, res) {
    gputility.excuteGP(getGP(req, res));
});

router.post('/', function(req, res) {
    gputility.excuteGP(postGP(req, res));
});

function* getGP(req, res) {
    try {
        let cous = yield dbutility.couModel.getCouAll();
        res.render('./adm/srhCouLst', { rows: cous });
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

function* postGP(req, res) {
    try {
        let couTel = req.body.selectedItem;
        let cou = yield dbutility.couModel.getCouByCouTel(couTel);
        req.session.couDtl = cou;
        res.redirect('/srhCouDtl');
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

module.exports = router;